//
//  HEPlusTextViewCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/19.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEPlusTextViewCell.h"

@interface HEPlusTextViewCell ()<QMUITextViewDelegate>

@property(nonatomic, strong) QMUITextView *textView;

@property (nonatomic, strong) UIButton *leftLabel;

@property (nonatomic, assign) CGFloat cellHeight;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation HEPlusTextViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.qmui_borderPosition = QMUIViewBorderPositionBottom;
        self.qmui_borderWidth = 1;
        self.qmui_borderColor = CSplitLineColor;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    _leftLabel = [UIButton new];
    [_leftLabel setTitle:@"左侧标题" forState:UIControlStateNormal];
    [self.contentView addSubview:_leftLabel];
    _leftLabel.userInteractionEnabled = NO;
    _leftLabel.titleLabel.font = DYFont(16);
    _leftLabel.imageEdgeInsets = UIEdgeInsetsMake(0,-15, 0, 0);
    [_leftLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_leftLabel setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
    [_leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    
    
    _textView = [QMUITextView  new];
    // 限制可输入的字符长度
    
    self.textView.placeholder = @"请填写备注信息，最多输入500个字!";
    self.textView.placeholderColor = CNavSubTitleColor; // 自定义 placeholder 的颜色
    self.textView.textContainerInset = UIEdgeInsetsMake(5, 7, 5, 7);
    self.textView.typingAttributes = @{NSFontAttributeName: UIFontMake(16),
                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
    };
    _textView.backgroundColor = CSplitLineColor;
    [self.contentView addSubview:_textView];
    self.textView.layer.cornerRadius = 4;
    [_textView setDelegate:self];
    
    
    [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.width.mas_offset([@"左侧标题" widthForFont:DYFont(16)] + 30);
    }];
    
    // 自适应title长度
    [_leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [_leftLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.equalTo(_leftLabel.mas_right).mas_offset(10); 
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.bottom.equalTo(self.contentView).mas_offset(-15);
    }];
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
 
    [_leftLabel setTitle:fiterModel.leftTitle forState:UIControlStateNormal];
    [_leftLabel setImage:(fiterModel.isHasMustSigns?[UIImage imageNamed:@"3"]: nil) forState:UIControlStateNormal];
    
    _textView.placeholder = fiterModel.placeHolder;
    _textView.text = [formModel valueForKey:fiterModel.tableKey];
    
    self.fiterModel.cellHeight = _cellHeight;

}


#pragma mark - <QMUITextViewDelegate>
// 实现这个 delegate 方法就可以实现自增高
- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height {
    if (height > (60-30)) {
        _cellHeight = height + 30;
       self.fiterModel.cellHeight = _cellHeight;
       [self refreshCellHeight];
    }
}

- (void)textView:(QMUITextView *)textView didPreventTextChangeInRange:(NSRange)range replacementText:(NSString *)replacementText {
    [Dialog toast:[NSString stringWithFormat:@"文字不能超过 %@ 个字符", @(textView.maximumTextLength)] delay:2.0];
}
 
- (void)textViewDidChange:(UITextView *)textView {
    [self.formModel setValue:textView.text forKey:self.fiterModel.tableKey]; // 将textField中的值赋给_formModel
}

- (UITableView *)tableView {
    if (!_tableView) {
        // 寻找Cell所属的tableView
        UIView *tableView = self.superview;
        while (![tableView isKindOfClass:[UITableView class]] && tableView) {
            tableView = tableView.superview;
        }
        _tableView = (UITableView *)tableView;
    }
    return _tableView;
}


- (void)refreshCellHeight {
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

@end
